﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyLambda
{
    public delegate void NoReturnNoParamterOutClass();
    //重载了一下 Action 
    public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14, in T15, in T16, in T17>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16,T17 arg17);
    public class LambdaShow
    {
        public delegate void NoReturnNoPara();
        public delegate int WithReturnNoPara();
        public delegate void NoReturnWithPara(int id, string name);
        public delegate LambdaShow WithReturnWithPara(DateTime time);

        public void Show()
        {
            string nameOut = "hello";

            {
                Student student = new Student()
                {
                    Id = 1,
                    Name = "Albert",
                    Age = 28,
                    ClassId = 2
                };

                student.Study();
            }

            {
                //.framework 1.1
                NoReturnNoPara method = new NoReturnNoPara(this.DoNothing); //委托的实例化
                method.Invoke();  //委托的调用
            }

            {

                NoReturnNoPara method = this.DoNothing;
                method.Invoke();
            }

            {
                //.framework 1.1
                NoReturnWithPara method = new NoReturnWithPara(this.DoNothing); //委托的实例化
                method.Invoke(1, "Albert");  //委托的调用
            }

            {
                //.framework 2.x
                NoReturnWithPara method = new NoReturnWithPara(
                    delegate (int id, string name)
                    {
                        Console.WriteLine(nameOut);
                        Console.WriteLine("{0} {1} DoNothing", id, name);
                    }
                    );
                method.Invoke(2, "yoyo");  //委托的调用
            }

            {
                //.framework 3.x  lambda 表达式
                NoReturnWithPara method = new NoReturnWithPara(
                    (int id, string name) =>
                    {
                        Console.WriteLine(nameOut);
                        Console.WriteLine("{0} {1} DoNothing", id, name);
                    }
                    );
                method.Invoke(3, "Ten");  //委托的调用
            }

            {
                //.framework 4.x  lambda 表达式
                NoReturnWithPara method = new NoReturnWithPara((id,name) =>Console.WriteLine("{0} {1} DoNothing", id, name));
                method.Invoke(4, "ABC");  //委托的调用
            }

            {
                //.framework 4.x  lambda 表达式
                NoReturnWithPara method = (id, name) => Console.WriteLine("{0} {1} DoNothing", id, name);
                method.Invoke(5, "VIP");  //委托的调用
            }

            {
                //0到16个参数，无返回值的委托
                Action act1 = () => Console.WriteLine("Action delegate");
                Action<int> act2 = t => { };
                Action<int, string, NoReturnWithPara, int, string, NoReturnWithPara, int, string, NoReturnWithPara, int, string, NoReturnWithPara, int, string, NoReturnWithPara, int> act3 = null;
                Action<int, string, NoReturnWithPara, int, string, NoReturnWithPara, int, string, NoReturnWithPara, int, string, NoReturnWithPara, int, string, NoReturnWithPara, int,string> act4 = null;

            }

            {
                //0到16个参数，带返回值的委托
                Func<int> func = () => 1;
                int i = func();
                Func<int, string> func2 = t => "test";
                string s = func2(1);
                Func<int, string, NoReturnWithPara, int, string, NoReturnWithPara, int, string, NoReturnWithPara, int, string, NoReturnWithPara, int, string, NoReturnWithPara, int> func3 = null;
            }
        }

        private void DoNothing()
        {
            Console.WriteLine("DoNothing");
        }

        private void DoNothing(int id, string name)
        {
            Console.WriteLine("{0} {1} DoNothing", id, name);
        }
    }
}
